<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class purchaser extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		
		// check if salesman logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('people/purchaser_model');
		$this->load->model('people/salesman_model');		
		$this->load->library('ion_auth');
		$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');

		// Load MongoDB library instead of native db driver if required
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
		$this->load->library('datatables');	
	}
	
	function get(){
		echo $this->purchaser_model->get();
	}
		
	
	function create_purchaser()
	{
		$this->data['page_title'] = "Create Purchaser";
		$this->data['branches']=$this->salesman_model->get_branch();
		if($this->data['branches'] == ''){
			$this->data['branches']=array();
			}
		//$this->data['companies']=$this->salesman_model->get_company();
		
		/*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}for checking the authentication*/

		//validate form input
		$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
			$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
		$this->form_validation->set_rules('id_branch', 'Branch', 'required|is_natural_no_zero|xss_clean');
		//$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
		$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
	//	$this->form_validation->set_rules('id_branch', 'Branch', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('email', 'Purchase Price', 'required|xss_clean');
		$this->form_validation->set_rules('address', 'Selling Price', 'required|xss_clean');
		if ($this->form_validation->run() == true)
		{
			
			$username = strtoupper($this->input->post('first_name')) . ' ' . strtoupper($this->input->post('last_name'));
			$email    = strtoupper($this->input->post('email'));
			$password = $this->input->post('password');
			$address  = strtoupper($this->input->post('address'));
			$company   = strtoupper($this->input->post('id_branch'));
			//$group_id = 2;
			//echo $company;exit;
			$additional_data = array(
				'first_name' =>strtoupper( $this->input->post('first_name')),
				'last_name'  => strtoupper($this->input->post('last_name')),
				'company'    => strtoupper($this->input->post('id_branch')),
				'phone'      => $this->input->post('phone_no'),
				'address' => strtoupper($this->input->post('address')),
				'group_id' =>'3'
			);
		//	print_r($additional_data);exit;
			
		
		$user_data = array(
				'ip_address' =>	$_SERVER['REMOTE_ADDR'],
				'username' => strtoupper($this->input->post('name')),
				'password' => $this->input->post('password'),
				'email'  => strtoupper($this->input->post('email')),
			);

		
		
		
		
		//	print_r($additional_data);exit;
			
		}
		
		//echo 'x'.$email;exit;
		if ($this->form_validation->run() == true && $this->ion_auth->register( $username, $password, $email, $additional_data) ) 
		{
			
			if(isset($flag)){
					redirect("purchase/purchase/add_purchase");
		
			}else{
					redirect("people/purchaser/get_all_purchaser");
			}
			
			
		}
		else
		{
			//display the create salesman form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['first_name'] = array(
				'name'  => 'first_name',
				'id'    => 'first_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('first_name'),
			);
			$this->data['last_name'] = array(
				'name'  => 'last_name',
				'id'    => 'last_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('last_name'),
			);
			
			$this->data['id_branch'] = array(
				'name'  => 'id_branch',
				'id'    => 'id_branch',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('id_branch'),
			);
			$this->data['password'] = array(
				'name'  => 'password',
				'id'    => 'password',
				'type'  => 'password',
				'value' => $this->form_validation->set_value('password'),
			);
			$this->data['password_confirm'] = array(
				'name'  => 'password_confirm',
				'id'    => 'password_confirm',
				'type'  => 'password',
				'value' => $this->form_validation->set_value('password_confirm'),
			);
			$this->data['phone_no'] = array(
				'name'  => 'phone_no',
				'id'    => 'phone_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('phone_no'),
			);

			$this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
			
			$this->data['address'] = array(
				'name'  => 'address',
				'id'    => 'address',
				'type'  => 'textarea',
				'value' => $this->form_validation->set_value('address'),
			);

		//	print_r($this->data);exit;

			$this->load->view('common/header');
			$this->load->view('people/purchaser/create_purchaser', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function get_all_purchaser(){
		//list the salesmans
		//echo "i am here";
		
		$this->data['users'] = $this->purchaser_model->get_purchaserList()->result();
			//print_r($this->data['users']);exit;
			//print_r($this->data['salesmans']);exit; 
	// $this->data['salesman_type'] =  $this->ion_auth_model->get_salesman_type($this->session->salesmandata('salesman_id'))->row();
	  //echo $get_salesman_type->name;
	   
	  // exit;
	   $meta['page_title'] = "Purchaser List";
	   $this->data['message'] = "Purchaser Added Successfully";
	 //  $this->session->set_flashdata('message', 'salesman Created Successfully');
	   $this->load->view('common/header', $meta);
	   $this->load->view('people/purchaser/view_purchaser1');
       $this->load->view('common/footer');
	   
		}
	

	function edit_purchaser($id)
	{
	
	$this->data['page_title'] = "Edit Purchaser";

	if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}
		$this->data['branches']=$this->purchaser_model->get_branch();
		$user = $this->ion_auth->user($id)->row();
		$groups=$this->ion_auth->groups()->result_array();
		$currentGroups = $this->ion_auth->get_users_groups($id)->result();
		
		//getting the user type ex. team lead 
	//	$get_user_type = $this->ion_auth_model->user_type()->result_array();
		// $currentUser = $this->ion_auth_model->get_user_type($id)->result();
		
		//getting the user type ex. fedonic 
		//$get_departments = $this->ion_auth_model->departments()->result_array();
		// $currentDepartments = $this->ion_auth_model->get_user_department($id)->result();
		
		//process the phone number
		//validate form input
		$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean|min_length[10]|max_length[10]');
		/*$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
		$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');*/
		
		//$this->form_validation->set_rules('groups', 'Groups', 'xss_clean');

		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/
			$username = strtoupper($this->input->post('first_name')) . ' ' . strtoupper($this->input->post('last_name'));

			$data = array(
				'username'  => $username,
				'company'    => strtoupper($this->input->post('id_branch')),
				'phone'      => $this->input->post('phone_no'),
				'email' =>	strtoupper($this->input->post('email')),
				'address' =>strtoupper(	$this->input->post('address')),
			);
			//print_r($data);exit;

	//update the password if it was posted
			/*if ($this->input->post('password'))
			{
				$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
				$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');

				$data['password'] = $this->input->post('password');
			}*/

			if ($this->form_validation->run() === TRUE)
			{
				$this->ion_auth->update($user->id, $data);

				//check to see if we are creating the user
				//redirect them back to the admin page
				$this->session->set_flashdata('message', "Salesman Saved");
					redirect("people/salesman/get_all_salesman");
			}
		}

		//display the edit user form
		$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
		$this->data['first_name'] = array(
			'name'  => 'first_name',
			'id'    => 'first_name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('first_name', $user->first_name),
		);
		$this->data['last_name'] = array(
			'name'  => 'last_name',
			'id'    => 'last_name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('last_name', $user->last_name),
		);
		$this->data['id_branch'] = array(
			'name'  => 'id_branch',
			'id'    => 'id_branch',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_branch', $user->company),
		);
		$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no', $user->phone),
		);
		/*$this->data['phone2'] = array(
			'name'  => 'phone2',
			'id'    => 'phone2',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
		);
		$this->data['phone3'] = array(
			'name'  => 'phone3',
			'id'    => 'phone3',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
		);*/
		
		$this->data['email'] = array(
			'name' => 'email',
			'id'   => 'email',
			'type' => 'text',
			'value' => $this->form_validation->set_value('email', $user->email),
		);
		
		$this->data['password'] = array(
			'name' => 'password',
			'id'   => 'password',
			'type' => 'password'
		);
		$this->data['password_confirm'] = array(
			'name' => 'password_confirm',
			'id'   => 'password_confirm',
			'type' => 'password'
		);
		$this->data['address'] = array(
			'name' => 'address',
			'id'   => 'address',
			'type' => 'textarea',
			'value' => $this->form_validation->set_value('address', $user->address),
		);
		
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Save Edit',
			'class' =>'btn btn-success'
		);
		
		
		 $this->data['user'] = $user;
		 $this->data['page_title'] = "Edit Purchaser";
		 $this->load->view('common/header',$this->data);
		 $this->load->view('people/purchaser/edit_purchaser', $this->data);
		 $this->load->view('common/footer');
	}


	function delete_purchaser($id){
		
		$this->purchaser_model->delete($id);
		redirect("people/purchaser/get_all_purchaser");
	
	}
	
		
	
	function check_salesman(){
		if($this->salesman_model->check_salesman($this->input->post('name'),$this->input->post('group'))){
					$this->form_validation->set_message('check_salesman', 'salesman Exists With The same group');
					return FALSE;
			}else{
				return TRUE;
		}
	
		}
	
	
	
		function _get_csrf_nonce()
	{
		$this->load->helper('string');
		$key   = random_string('alnum', 8);
		$value = random_string('alnum', 20);
		$this->session->set_flashdata('csrfkey', $key);
		$this->session->set_flashdata('csrfvalue', $value);

		return array($key => $value);
	}

	function _valid_csrf_nonce()
	{
		if ($this->input->post($this->session->flashdata('csrfkey')) !== FALSE &&
			$this->input->post($this->session->flashdata('csrfkey')) == $this->session->flashdata('csrfvalue'))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	
	
	
	
	}
	

?>